import request from '@/utils/request'
import isEmpty from 'lodash/isEmpty'
import { getModel } from '@ve/utils/utils'
//必须带done回调 执行完成调用
export default function submitForm({ vm, action, done }) {
  console.log('提交表单')
  console.log('action', action.extension.submitForm)
  let { api, method } = action.extension.submitForm
  console.log(api, method)
  // let getNowPageData = vm.$store.getters['preview/getNowPageData']
  // console.log(getNowPageData)
  // let form = {}
  // getNowPageData.pageModules.forEach((row) => {
  //   if (row._id == action.extension.submitForm.value[0]) {
  //     form = row.data.formData
  //   }
  // })
  let _componentId = action.extension.submitForm.value[0]
  let { formData } = getModel(vm.$store.getters['preview/getNowPageData']['pageModules'], _componentId)
  console.log(formData)
  //发起请求
  request({
    url: api,
    method: 'post',
    data: {
      data: JSON.stringify(formData),
      method: method,
    },
  }).then((res) => {
    console.log(res)
    //解密
    // console.log(decryptd(res))
    // if (type === 2) {
    formData = isEmpty(formData) ? res.data : res.data[formData]
    // }

    // let getNowPageData = vm.$store.getters['preview/getNowPageData']
    // getNowPageData.data = res
    done(null, res)
  })
  // done()
}
